package com.srwl.mytx.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.domain.OrderReceiveInfo;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.OrderReceiveInfoAdapter;
import com.srwl.mytx.parse.UserProfileManager;

import java.util.List;


public class OrderReceiveInfoSelector extends DialogFragment implements OrderReceiveInfoAdapter.InteractionListener {

    String TAG = OrderReceiveInfoSelector.class.getSimpleName();
    private List<OrderReceiveInfo> orderReceiveInfoList;
    private RecyclerView rv_orderReceiveInfoList;
    private OrderReceiveInfoAdapter orderReceiveInfoAdapter;
    private InteractionListener listener;

    public static OrderReceiveInfoSelector newInstance() {
        OrderReceiveInfoSelector fragment = new OrderReceiveInfoSelector();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderReceiveInfoList = UserProfileManager.getInstance().getLoginUserInfo().getOrderReceiveInfoList();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.order_receive_info_select_layout, container, false);
        rv_orderReceiveInfoList = view.findViewById(R.id.rv_order_receive_info_list);
        rv_orderReceiveInfoList.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL,false));
        orderReceiveInfoAdapter = new OrderReceiveInfoAdapter(getActivity(), orderReceiveInfoList,this);
        rv_orderReceiveInfoList.setAdapter(orderReceiveInfoAdapter);
        return view;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.setContentView(R.layout.order_receive_info_select_layout);
        Window win = dialog.getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
       // win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams params = win.getAttributes();
       // params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
       // params.dimAmount = 0;
        win.setAttributes(params);
        return dialog;
    }

    @Override
    public void onItemDelete(int position) {
        orderReceiveInfoList.remove(position);
        UserProfileManager.getInstance().setOrderReceiveInfoList(orderReceiveInfoList);
        if (listener!=null){
            listener.onItemDelete(orderReceiveInfoList);
        }

    }

    @Override
    public void onItemSelect(OrderReceiveInfo info) {
        if (listener!=null){
            listener.onItemSelect(info);
        }
        dismiss();
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public interface InteractionListener {

        void onItemDelete(List<OrderReceiveInfo> orderReceiveInfoList);

        void onItemSelect(OrderReceiveInfo info);

    }
}
